package com.samp.lucene.func.userdefined;

import java.io.IOException;
import java.util.Map;

import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.queries.function.FunctionValues;
import org.apache.lucene.queries.function.ValueSource;
import org.apache.lucene.queries.function.docvalues.StrDocValues;
import org.apache.solr.common.SolrException;

/**
 * 主要作用是连接两个值。
 *
 */
public class ConcatenateFunction extends ValueSource {
	
	protected final ValueSource valueSource1;
	protected final ValueSource valueSource2;
	protected final String delimiter;
	
	public ConcatenateFunction(ValueSource valueSource1, ValueSource valueSource2, String delimiter){
		if( valueSource1 == null || valueSource2 == null ){
			throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,"missing inputs");
		}
		this.valueSource1 = valueSource1;
		this.valueSource2 = valueSource2;
		if( delimiter != null ){
			this.delimiter = delimiter;
		}else{
			this.delimiter = "";
		}
	}

	@SuppressWarnings("rawtypes")
	@Override
	public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
		final FunctionValues firstValues = valueSource1.getValues(context, readerContext);
		final FunctionValues secondValues = valueSource2.getValues(context, readerContext);
		return new StrDocValues( this ){

			@Override
			public String strVal(int doc) {
				return firstValues.strVal(doc).concat(delimiter).concat(secondValues.strVal(doc));
			}
			
		};
	}

	@Override
	public boolean equals(Object o) {
		if( this.getClass() != o.getClass() )
			return false;
		ConcatenateFunction other = (ConcatenateFunction)o;
		return this.valueSource1.equals(other.valueSource1) 
				&& this.valueSource2.equals(other.valueSource2)
					&& this.delimiter.equals(other.delimiter);
	}

	@Override
	public int hashCode() {
		long combinedHashes;
		combinedHashes = (this.valueSource1.hashCode() + this.valueSource2.hashCode() +this.delimiter.hashCode());
		return (int)(combinedHashes ^ (combinedHashes >>> 32));
	}

	@Override
	public String description() {
		return " concatenatge two values ";
	}

}
